package cn.Liujialin.pet.controller;


import cn.Liujialin.basic.util.AjaxResult;
import cn.Liujialin.basic.util.LoginContext;
import cn.Liujialin.basic.util.PageList;
import cn.Liujialin.cation.BusinessException;
import cn.Liujialin.pet.domain.Pet;
import cn.Liujialin.pet.query.PetQuery;
import cn.Liujialin.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;


    @PostMapping("/adopt")
    public AjaxResult adopt(@RequestBody Map<String,Long> param, HttpServletRequest request){
        try {
            petService.adopt(param, LoginContext.getCurrentLoginInfo(request));
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("领养失败！");
        }
    }


    // 查询宠物信息 + 详情

    @GetMapping("/detail/{petId}")
    public Pet queryDetailById(@PathVariable("petId") Long petId){
        return petService.queryDetailById(petId);
    }

    // 改接口支持前端和后端查询，请传入必要的条件
    @PostMapping
    public PageList<Pet> queryPage(@RequestBody PetQuery query, HttpServletRequest request){
        return petService.queryPage(query);
    }

    //宠物上架

    @PostMapping("/onsale")
    public AjaxResult onSale(@RequestBody List<Long> ids){
        try {
            petService.onSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败！");
        }
    }

    // 宠物下架

    @PostMapping("/offsale")
    public AjaxResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.fail("删除失败！");
        }
    }

}
